//
//  LKConfirmCustomInforViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/3.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKConfirmCustomInforViewController.h"
#import "LKWorkOrderListDetailCell.h"
#import "LKWorkOrderListDetailPictureCell.h"
#import "LKOrderEditBottomView.h"
#import "LKOrderEditTopView.h"
#import "LKAddWorkCustomViewController.h"
#import "LKChooseProductViewController.h"


@interface LKConfirmCustomInforViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)NSMutableArray *titlesArray;
@property(nonatomic,strong)LKOrderEditBottomView *bottomView;

@end

@implementation LKConfirmCustomInforViewController
-(LKAddOrderModel *)addOrderModel{
    if (!_addOrderModel) {
        _addOrderModel = [[LKAddOrderModel alloc] init];
    }
    return _addOrderModel;
}
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        WS(weakSelf)
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditBottomView" owner:self options:nil] lastObject];
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(69);
        }];
        [[_bottomView.forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
        [[_bottomView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            LKChooseProductViewController *vc = [[LKChooseProductViewController alloc] init];
            vc.customerModel = weakSelf.model;
            weakSelf.addOrderModel.CustomerName = weakSelf.model.customerName;
            weakSelf.addOrderModel.CustomerCode = weakSelf.model.customerCode;
            weakSelf.addOrderModel.CustomerId = weakSelf.model.customerId;
            weakSelf.addOrderModel.IdentitfyCategory = weakSelf.model.identitfyCategory;
            weakSelf.addOrderModel.Address = weakSelf.model.address;
            weakSelf.addOrderModel.IdentitfyPhotoUrl = weakSelf.model.identitfyPhotoUrl;
            weakSelf.addOrderModel.IdentitfyBackPhotoUrl = weakSelf.model.identitfyBackPhotoUrl;
            weakSelf.addOrderModel.IdentityId = weakSelf.model.identityId;
            weakSelf.addOrderModel.IdentitfyPhotoUrl = weakSelf.model.identitfyPhotoUrl;
            weakSelf.addOrderModel.IdentitfyBackPhotoUrl = weakSelf.model.identitfyBackPhotoUrl;
            vc.addOrderModel = weakSelf.addOrderModel;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10 - 70) style:UITableViewStylePlain registercellClasss:@[@"LKWorkOrderListDetailCell",@"LKWorkOrderListDetailPictureCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.isOrderUpdate) {
        self.tableview.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT + 10, WIDTH , HEIGHT - NAVIGATION_BAR_HEIGHT - 10);
        [self.bottomView removeFromSuperview];
        NSString *customerName = [NSString stringWithFormat:@"%@%@",@"客户名称：",self.addOrderModel.CustomerName];
        NSString *customerCode = [NSString stringWithFormat:@"%@%@",@"客户编号：",self.addOrderModel.CustomerCode];
        NSString *identCatory = [NSString stringWithFormat:@"%@%@",@"证件类型：",[NSString backTitleAccordingIdCatogaryType:[self.addOrderModel.IdentitfyCategory integerValue]]];
        NSString *identId = [NSString stringWithFormat:@"%@%@",@"证件号码：",self.addOrderModel.IdentityId];
        NSString *Address = [NSString stringWithFormat:@"%@",[NSString judegeIsNullAndTransform:self.addOrderModel.Address]];
        NSString *addre = [NSString stringWithFormat:@"%@%@",@"通讯地址：",Address];
        [self.titlesArray addObjectsFromArray:@[customerName,customerCode,identCatory,identId,addre,@"证件正面：",@"证件背面："]];
        [self.tableview reloadData];
    }else{
        [self getOrderCustomerInformation];
    }
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerReloadData) name:@"AddcustomerReloadTableview" object:nil];
    // Do any additional setup after loading the view.
}
-(void)headerReloadData{
    [self.titlesArray removeAllObjects];
    [self getOrderCustomerInformation];
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"确认客户信息";
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titlesArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LKWorkOrderListDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailCell"];
    cell.confirmImageView.hidden = YES;
    cell.arrowBtn.hidden = YES;
    cell.lineView.hidden = YES;
    if (indexPath.row == 0) {
        cell.confirmImageView.hidden = NO;
        if ([_model.isAuthenticated boolValue]) {
            cell.confirmImageView.image = [UIImage imageNamed:@"confirmPeople"];
        }else{
           cell.confirmImageView.image = [UIImage imageNamed:@"unconfirmPeople"];
        }
        cell.lineView.hidden = NO;
    }
    if (indexPath.row == 5 || indexPath.row == 6) {
        LKWorkOrderListDetailPictureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkOrderListDetailPictureCell"];
        cell.titleLabel.text = self.titlesArray[indexPath.row];
        if (indexPath.row == 5) {
            if (self.isOrderUpdate) {
             [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.IdentitfyPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }else{
              [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.model.identitfyPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }

        }else{
            if (self.isOrderUpdate) {
            [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.addOrderModel.IdentitfyBackPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }else{
             [cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.model.identitfyBackPhotoUrl]] placeholderImage:[UIImage imageNamed:@"pic"]];
            }

        }
        return cell;
    }
    cell.titileLabel.text = self.titlesArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 5 || indexPath.row == 6) {
        return 90;
    }
    return 35;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    WS(weakSelf)
    LKOrderEditTopView *topView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditTopView" owner:self options:nil] lastObject];
    [[topView.rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        LKAddWorkCustomViewController *vc = [[LKAddWorkCustomViewController alloc] init];
        vc.updateType = YES;
        vc.updateModel = weakSelf.model;
        vc.updateModel.customerId = weakSelf.model.customerId;
        vc.isOrderUpdate = weakSelf.isOrderUpdate;
        vc.addOrderModel = weakSelf.addOrderModel;
        if ([_model.changeEdit integerValue] == 0) {
            vc.headerTitle = @"编辑";
            [SVProgressHUD showInfoWithStatus:@"客户信息暂不能编辑"];
            return ;
        }else if ([_model.changeEdit integerValue] == 1){
            vc.headerTitle = @"编辑";
        }else{
            vc.headerTitle = @"变更";
        }
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    return topView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 43;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor whiteColor];
    return footerView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000000000000001;
}

-(NSMutableArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = [[NSMutableArray alloc] init];
    }
    return _titlesArray;
}

/**
 获取订单客户的详细信息
 */
-(void)getOrderCustomerInformation{
    [SVProgressHUD showWithStatus:@"数据加载中..."];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:_model.customerCode,@"customerCode", nil];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:getAddOrderBasicUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        LKOrderModel *model = [LKOrderModel modelWithDictionary:responseObject[@"result"]];
        self.addOrderModel.OwnerName = model.ownerName;
        self.addOrderModel.Director = model.director;
        self.addOrderModel.SalesDepartment = model.salesDepartment;
        self.addOrderModel.BranchCompany = model.branchCompany;
        self.addOrderModel.Area = model.area;
        self.addOrderModel.OwnerDepartmentCode = model.ownerDepartmentCode;
        self.addOrderModel.Owner = model.owner;
        self.addOrderModel.ChangeEdit = model.changeEdit;
        self.addOrderModel.BankCardTel = model.bankCardTel;
        self.addOrderModel.CusIsAuthenticated = model.cusIsAuthenticated;
        _model.customerName = model.customerName;
        _model.customerCode = model.customerCode;
        _model.identitfyCategory = model.identitfyCategory;
        _model.identityId = model.identityId;
        _model.address = model.address;
        _model.identitfyPhotoUrl = model.identitfyPhotoUrl;
        _model.identitfyBackPhotoUrl = model.identitfyBackPhotoUrl;
        _model.changeEdit = model.changeEdit;
        _model.customerId = model.customerId;
        _model.isAuthenticated = responseObject[@"result"][@"isAuthenticated"];
        self.addOrderModel.CusIsAuthenticated = responseObject[@"result"][@"isAuthenticated"];
        NSString *customerName = [NSString stringWithFormat:@"%@%@",@"客户名称：",_model.customerName];
        NSString *customerCode = [NSString stringWithFormat:@"%@%@",@"客户编号：",_model.customerCode];
        NSString *identCatory = [NSString stringWithFormat:@"%@%@",@"证件类型：",[NSString backTitleAccordingIdCatogaryType:[_model.identitfyCategory integerValue]]];
        NSString *identId = [NSString stringWithFormat:@"%@%@",@"证件号码：",_model.identityId];
        NSString *Address = [NSString stringWithFormat:@"%@",[NSString judegeIsNullAndTransform:_model.address]];
        NSString *addre = [NSString stringWithFormat:@"%@%@",@"通讯地址：",Address];
        if ([self.model.identitfyCategory isEqual:@"1"]) {
            if ([self.model.isAuthenticated boolValue]) {
                [self.titlesArray addObjectsFromArray:@[customerName,customerCode,identCatory,identId,addre]];
            }else{
                [self.titlesArray addObjectsFromArray:@[customerName,customerCode,identCatory,identId,addre,@"证件正面：",@"证件背面："]];
            }

        }else{
            [self.titlesArray addObjectsFromArray:@[customerName,customerCode,identCatory,identId,addre,@"证件正面：",@"证件背面："]];
        }

        [self.tableview reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
